import QtQuick 6.8
import QtQuick.Controls 6.8
import QtQuick.Layouts 6.8

Rectangle {
    color: "lightgreen"
    width: parent.width
    height: parent.height

    Rectangle {
        id: titleBar
        color: "grey"
        width: parent.width
        height: 60
        anchors.top: parent.top

        Text {
            text: "自动养龙助手"
            font.pointSize: 16
            height: parent.height
            verticalAlignment: Text.AlignVCenter
            anchors.left: parent.left
            anchors.leftMargin: 20
        }
    }

    // 取龙的备注
    RowLayout {
        id: takeDinosaurRemark
        anchors.top: titleBar.bottom
        anchors.topMargin: 10
        anchors.horizontalCenter: parent.horizontalCenter // 水平居中
        spacing: 10

        Text {
            text: "取龙备注: "
            font.pointSize: 12
        }

        // 输入框
        TextField {
            id: takeDinosaurRemarkInput
            Layout.preferredWidth: 200
            placeholderText: "请输入取龙备注"
        }
    }

    // 养龙种类
    RowLayout {
        id: takeDinosaurType
        anchors.top: takeDinosaurRemark.bottom
        anchors.topMargin: 10
        anchors.horizontalCenter: parent.horizontalCenter // 水平居中
        spacing: 10

        Text {
            text: "养龙种类: "
            font.pointSize: 12
        }

        // 选择框
        ComboBox {
            id: takeDinosaurTypeInput
            Layout.preferredWidth: 200
            model: ["高脊龙","异特龙",
            "牛龙",
            "暴君龙",
            "棘背龙",
            "始盗龙",
            "霸王龙",
            "南方巨兽龙",
            "包头龙",
            "龙王龙",
            "剑龙",
            "三角龙",
            "豪勇龙",]
            currentIndex: 0
        }
    }

    // 饲养数量
    RowLayout {
        id: takeDinosaurQuantity
        anchors.top: takeDinosaurType.bottom
        anchors.topMargin: 10
        anchors.horizontalCenter: parent.horizontalCenter // 水平居中
        spacing: 10

        Text {
            text: "饲养数量: "
            font.pointSize: 12
        }

        // 输入框
        TextField {
            id: takeDinosaurQuantityInput
            Layout.preferredWidth: 200
            placeholderText: "请输入饲养数量"
            inputMethodHints: Qt.ImhDigitsOnly
        }
    }

    // 最高等级
    RowLayout {
        id: takeDinosaurMaxLevel
        anchors.top: takeDinosaurQuantity.bottom
        anchors.topMargin: 10
        anchors.horizontalCenter: parent.horizontalCenter // 水平居中
        spacing: 10

        Text {
            text: "最高等级: "
            font.pointSize: 12
        }

        // 选择框
        ComboBox {
            id: takeDinosaurMaxLevelInput
            Layout.preferredWidth: 200
            model: ["成年", "少年" ]
            currentIndex: 0
        }
    }

    // 存龙备注
    RowLayout {
        id: storeDinosaurRemark
        anchors.top: takeDinosaurMaxLevel.bottom
        anchors.topMargin: 10
        anchors.horizontalCenter: parent.horizontalCenter // 水平居中
        spacing: 10

        Text {
            text: "存龙备注: "
            font.pointSize: 12
        }

        // 输入框
        TextField {
            id: storeDinosaurRemarkInput
            Layout.preferredWidth: 200
            placeholderText: "请输入存龙备注"
        }
    }

    //  启动自动养龙按钮
    Button {
        id: saveButton
        text: "启动自动养龙"
        anchors.top: storeDinosaurRemark.bottom
        anchors.topMargin: 20
        anchors.horizontalCenter: parent.horizontalCenter // 水平居中
        // anchors.left: storeDinosaurRemark.left
        // anchors.right: storeDinosaurRemark.right
        height: 30
        onClicked: {
            // 检查输入框是否为空
            if (takeDinosaurRemarkInput.text === "" || takeDinosaurTypeInput.currentText === "" || takeDinosaurQuantityInput.text === "" || takeDinosaurMaxLevelInput.currentText === "" || storeDinosaurRemarkInput.text === "") {
                addLog("warning: 请填写所有必填项！");
                return;
            }

            // 禁用按钮 
            saveButton.enabled = false;

            // 禁用输入框
            takeDinosaurRemarkInput.enabled = false;
            takeDinosaurTypeInput.enabled = false;
            takeDinosaurQuantityInput.enabled = false;
            takeDinosaurMaxLevelInput.enabled = false;
            storeDinosaurRemarkInput.enabled = false;

            addLog("info: 自动养龙助手已启动，请等待...");

            console.log(takeDinosaurRemarkInput.text);
            console.log(storeDinosaurRemarkInput.text);
            console.log(takeDinosaurTypeInput.currentText);
            console.log(takeDinosaurQuantityInput.text);
            console.log(takeDinosaurMaxLevelInput.currentText);

            // 调用自动养龙函数
            breedDinosaur.breed_dinosaur(takeDinosaurRemarkInput.text, storeDinosaurRemarkInput.text, takeDinosaurTypeInput.currentText, takeDinosaurQuantityInput.text, takeDinosaurMaxLevelInput.currentText);
        }
    }


    // 日志显示区域
    // 使用 ScrollView 包裹 TextArea
    ScrollView {
        id: logView
        anchors.top: saveButton.bottom
        anchors.bottom: parent.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.margins: 10

        TextArea {
            id: logArea
            width: parent.width
            wrapMode: TextArea.Wrap
            readOnly: true
        }
    }

    // 连接 Python 信号到 QML 槽函数
    Connections {
        target: outputRedirector
        function onTextWritten(message) {
            addLog(message)
        }
    }

    // 冻结输入框
    Connections {
        target: systemStatus
        function onSystemStatusSignal(message) {
            if (message === "未知龙种") {
                unfreezeInput();
                addLog("info: 程序已停止，请检查龙种是否正确。");
            }
            else if (message === "窗口未找到") {
                unfreezeInput();
                addLog("info: 程序已停止，请检查游戏是否已打开。");
            }
        }
    }



    // 解冻输入框
    function unfreezeInput() {
        takeDinosaurRemarkInput.enabled = true;
        takeDinosaurTypeInput.enabled = true;
        takeDinosaurQuantityInput.enabled = true;
        takeDinosaurMaxLevelInput.enabled = true;
        storeDinosaurRemarkInput.enabled = true;
        saveButton.enabled = true;
        saveButton.text = "启动自动养龙";
    }


    function addLog(message) {
        var currentText = logArea.text;
        if (currentText.length > 0) {
            currentText += "\n";
        }
        currentText += message;

        // 将文本按行分割成数组
        var lines = currentText.split("\n");
        // 如果行数超过 200，移除最早的行
        if (lines.length > 200) {
            lines = lines.slice(lines.length - 200);
        }
        // 将处理后的行重新组合成文本
        logArea.text = lines.join("\n");
        // 自动滚动到底部
        // 自动滚动到底部
        logView.contentItem.contentY = logArea.contentHeight - logView.height;


    }




   
  
}